package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.GoodsEvaluateDetailGridAdapter;
import com.longcai.piaojia.conn.EvaluateDetailGet;
import com.longcai.piaojia.conn.EvaluateZanGet;
import com.longcai.piaojia.widget.GoodsEvaluateListView;
import com.longcai.piaojia.widget.NoScrollGridView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/7.
 */
public class GoodsEvaluateDetailActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.goods_evaluate_detail_iv) private ImageView goods_evaluate_detail_iv;
    @BoundView(R.id.goods_evaluate_detail_title_tv) private TextView goods_evaluate_detail_title_tv;
    @BoundView(R.id.goods_evaluate_detail_star_tv) private GoodsEvaluateListView goods_evaluate_detail_star_tv;
    @BoundView(R.id.goods_evaluate_detail_content_tv) private TextView goods_evaluate_detail_content_tv;
    @BoundView(R.id.goods_evaluate_detail_nsgv) private NoScrollGridView goods_evaluate_detail_nsgv;
    @BoundView(R.id.goods_evaluate_detail_time_tv) private TextView goods_evaluate_detail_time_tv;
    @BoundView(R.id.goods_evaluate_detail_zan_ll) private LinearLayout goods_evaluate_detail_zan_ll;
    @BoundView(R.id.goods_evaluate_detail_zan_iv) private ImageView goods_evaluate_detail_zan_iv;
    @BoundView(R.id.goods_evaluate_detail_number_tv) private TextView goods_evaluate_detail_number_tv;
    private List<EvaluateDetailGet.EvaluateDetailImgEntity> lists = new ArrayList<>();
    private GoodsEvaluateDetailGridAdapter goodsEvaluateDetailGridAdapter;
    private EvaluateDetailGet.EvaluateDetailEntity evaluateDetail;
    private String show_id, user_id, zambia_state, posttime;
    private int position;

    private EvaluateDetailGet evaluateDetailGet = new EvaluateDetailGet(new AsyCallBack<EvaluateDetailGet.EvaluateDetailEntity>() {

        @Override
        public void onSuccess(String toast, int type, EvaluateDetailGet.EvaluateDetailEntity evaluateDetailEntity) throws Exception {
            super.onSuccess(toast, type, evaluateDetailEntity);

            evaluateDetail = evaluateDetailEntity;

            goods_evaluate_detail_title_tv.setText(evaluateDetailEntity.nickname);
            goods_evaluate_detail_star_tv.setSelect(Integer.parseInt(evaluateDetailEntity.star));
            goods_evaluate_detail_content_tv.setText(evaluateDetailEntity.content);
            goods_evaluate_detail_time_tv.setText(evaluateDetailEntity.datetime);
            goods_evaluate_detail_number_tv.setText(evaluateDetailEntity.zambia);

//            lists.addAll(evaluateDetailEntity.img);
//            goodsEvaluateDetailGridAdapter.notifyDataSetChanged();

            GlideLoader.getInstance().get(GoodsEvaluateDetailActivity.this, evaluateDetailEntity.avatar, goods_evaluate_detail_iv, R.mipmap.mine_headimg, GlideLoader.TYPE_IMAGE_CIRCLE);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(GoodsEvaluateDetailActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    private EvaluateZanGet evaluateZanGet = new EvaluateZanGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            goods_evaluate_detail_number_tv.setText(String.valueOf(Integer.parseInt(evaluateDetail.zambia) + 1));

            goods_evaluate_detail_zan_iv.setImageResource(R.mipmap.evaluate_sel);

            GoodsEvaluateListActivity.GoodsEvaluateCallBack goodsEvaluateCallBack = (GoodsEvaluateListActivity.GoodsEvaluateCallBack) getAppCallBack(GoodsEvaluateListActivity.class);
            goodsEvaluateCallBack.refresh_zan(position);

            Toast.makeText(GoodsEvaluateDetailActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(GoodsEvaluateDetailActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_evaluate_detail);

        show_id = getIntent().getStringExtra("show_id");
        user_id = getIntent().getStringExtra("user_id");
        zambia_state = getIntent().getStringExtra("zambia_state");
        posttime = getIntent().getStringExtra("posttime");
        position = getIntent().getIntExtra("position", -1);

        setBackTrue();

        setTitleName(getResources().getString(R.string.goods_evaluate_detail_title_text));

        goodsEvaluateDetailGridAdapter = new GoodsEvaluateDetailGridAdapter(this, lists);

        goods_evaluate_detail_nsgv.setHorizontalSpacing(ScaleScreenHelperFactory.getInstance().getWidthHeight(37));
        goods_evaluate_detail_nsgv.setVerticalSpacing(ScaleScreenHelperFactory.getInstance().getWidthHeight(20));
        goods_evaluate_detail_nsgv.setAdapter(goodsEvaluateDetailGridAdapter);

        if(zambia_state.equals("1")){

            goods_evaluate_detail_zan_iv.setImageResource(R.mipmap.evaluate_sel);

        }else{

            goods_evaluate_detail_zan_iv.setImageResource(R.mipmap.evaluate_no);

        }

        goods_evaluate_detail_zan_ll.setOnClickListener(this);

        evaluateDetailGet.user_id = user_id;
        evaluateDetailGet.show_id = show_id;
        evaluateDetailGet.member_id = BaseApplication.BasePreferences.readUserId();
        evaluateDetailGet.posttime = posttime;
        evaluateDetailGet.execute(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.goods_evaluate_detail_zan_ll:

                if (!BaseApplication.BasePreferences.readIsLogin()){

                    startActivity(new Intent(GoodsEvaluateDetailActivity.this, LoginActivity.class));

                }else{

                    evaluateZanGet.member_id = BaseApplication.BasePreferences.readUserId();
                    evaluateZanGet.show_id = show_id;
                    evaluateZanGet.user_id = evaluateDetail.user_id;
                    evaluateZanGet.zambia = evaluateDetail.zambia;
                    evaluateZanGet.posttime = evaluateDetail.posttime;
                    evaluateZanGet.execute(GoodsEvaluateDetailActivity.this);

                }

                break;

        }

    }
}
